home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / pbm / source / netpbm-01mar94.patches.lha / ppm / bmp.h next >
Encoding:
C/C++ Source or Header  |  1994-05-22  |  5.2 KB  |  204 lines

  1. /*\
  2.  * $Id: bmp.h,v 1.4 1994/03/30 mpc $
  3.  *
  4.  * bmp.h - routines to calculate sizes of parts of BMP files
  5.  *
  6.  * Some fields in BMP files contain offsets to other parts
  7.  * of the file.  These routines allow us to calculate these
  8.  * offsets, so that we can read and write BMP files without
  9.  * the need to fseek().
  10.  *
  11.  * Copyright (C) 1992 by David W. Sanderson.
  12.  *
  13.  * Permission to use, copy, modify, and distribute this software and its
  14.  * documentation for any purpose and without fee is hereby granted,
  15.  * provided that the above copyright notice appear in all copies and
  16.  * that both that copyright notice and this permission notice appear
  17.  * in supporting documentation.  This software is provided "as is"
  18.  * without express or implied warranty.
  19.  *
  20.  * $Log: bmp.h,v $
  21.  * Revision 1.4  1994/03/30            Mark Paul Chackerian
  22.  * Fixed BMPlenrgbtable so that 24 bit BMPs (which have no rgb table)
  23.  * can now be converted to ppm.
  24.  *
  25.  * Revision 1.3  1992/11/24  19:39:56  dws
  26.  * Added copyright.
  27.  *
  28.  * Revision 1.2  1992/11/17  02:13:37  dws
  29.  * Adjusted a string's name.
  30.  *
  31.  * Revision 1.1  1992/11/16  19:54:44  dws
  32.  * Initial revision
  33.  *
  34. \*/
  35.  
  36. #ifndef _BMP_H_
  37. #define _BMP_H_
  38.  
  39. #include        "pbmplus.h"
  40.  
  41. /* prototypes */
  42. static unsigned long BMPlenfileheader ARGS((int class));
  43. static unsigned long BMPleninfoheader ARGS((int class));
  44. static unsigned long BMPlenrgbtable ARGS((int class, unsigned long bitcount));
  45. static unsigned long BMPlenline ARGS((int class, unsigned long bitcount, unsigned long x));
  46. static unsigned long BMPlenbits ARGS((int class, unsigned long bitcount, unsigned long x, unsigned long y));
  47. static unsigned long BMPlenfile ARGS((int class, unsigned long bitcount, unsigned long x, unsigned long y));
  48. static unsigned long BMPoffbits ARGS((int class, unsigned long bitcount));
  49. /*
  50.  * Classes of BMP files
  51.  */
  52.  
  53. #define C_WIN   1
  54. #define C_OS2   2
  55.  
  56. static char     er_internal[] = "%s: internal error!";
  57.  
  58. static unsigned long
  59. BMPlenfileheader(class)
  60.         int             class;
  61. {
  62.         switch (class)
  63.         {
  64.         case C_WIN:
  65.                 return 14;
  66.         case C_OS2:
  67.                 return 14;
  68.         default:
  69.                 pm_error(er_internal, "BMPlenfileheader");
  70.                 return 0;
  71.         }
  72. }
  73.  
  74. static unsigned long
  75. BMPleninfoheader(class)
  76.         int             class;
  77. {
  78.         switch (class)
  79.         {
  80.         case C_WIN:
  81.                 return 40;
  82.         case C_OS2:
  83.                 return 12;
  84.         default:
  85.                 pm_error(er_internal, "BMPleninfoheader");
  86.                 return 0;
  87.         }
  88. }
  89.  
  90. static unsigned long
  91. BMPlenrgbtable(class, bitcount)
  92.         int             class;
  93.         unsigned long   bitcount;
  94. {
  95.         unsigned long   lenrgb;
  96.  
  97.         if (bitcount < 1)
  98.         {
  99.                 pm_error(er_internal, "BMPlenrgbtable");
  100.                 return 0;
  101.         }
  102.         switch (class)
  103.         {
  104.         case C_WIN:
  105.                 lenrgb = 4;
  106.                 break;
  107.         case C_OS2:
  108.                 lenrgb = 3;
  109.                 break;
  110.         default:
  111.                 pm_error(er_internal, "BMPlenrgbtable");
  112.                 return 0;
  113.         }
  114.         if (bitcount == 24) {
  115.           return 0;
  116.         } else {
  117.           return (1 << bitcount) * lenrgb;
  118.         }
  119. }
  120.  
  121. /*
  122.  * length, in bytes, of a line of the image
  123.  *
  124.  * Evidently each row is padded on the right as needed to make it a
  125.  * multiple of 4 bytes long.  This appears to be true of both
  126.  * OS/2 and Windows BMP files.
  127.  */
  128. static unsigned long
  129. BMPlenline(class, bitcount, x)
  130.         int             class;
  131.         unsigned long   bitcount;
  132.         unsigned long   x;
  133. {
  134.         unsigned long   bitsperline;
  135.  
  136.         switch (class)
  137.         {
  138.         case C_WIN:
  139.                 break;
  140.         case C_OS2:
  141.                 break;
  142.         default:
  143.                 pm_error(er_internal, "BMPlenline");
  144.                 return 0;
  145.         }
  146.  
  147.         bitsperline = x * bitcount;
  148.  
  149.         /*
  150.          * if bitsperline is not a multiple of 32, then round
  151.          * bitsperline up to the next multiple of 32.
  152.          */
  153.         if ((bitsperline % 32) != 0)
  154.         {
  155.                 bitsperline += (32 - (bitsperline % 32));
  156.         }
  157.  
  158.         if ((bitsperline % 32) != 0)
  159.         {
  160.                 pm_error(er_internal, "BMPlenline");
  161.                 return 0;
  162.         }
  163.  
  164.         /* number of bytes per line == bitsperline/8 */
  165.         return bitsperline >> 3;
  166. }
  167.  
  168. /* return the number of bytes used to store the image bits */
  169. static unsigned long
  170. BMPlenbits(class, bitcount, x, y)
  171.         int             class;
  172.         unsigned long   bitcount;
  173.         unsigned long   x;
  174.         unsigned long   y;
  175. {
  176.         return y * BMPlenline(class, bitcount, x);
  177. }
  178.  
  179. /* return the offset to the BMP image bits */
  180. static unsigned long
  181. BMPoffbits(class, bitcount)
  182.         int             class;
  183.         unsigned long   bitcount;
  184. {
  185.         return BMPlenfileheader(class)
  186.                 + BMPleninfoheader(class)
  187.                 + BMPlenrgbtable(class, bitcount);
  188. }
  189.  
  190. /* return the size of the BMP file in bytes */
  191. static unsigned long
  192. BMPlenfile(class, bitcount, x, y)
  193.         int             class;
  194.         unsigned long   bitcount;
  195.         unsigned long   x;
  196.         unsigned long   y;
  197. {
  198.         return BMPoffbits(class, bitcount)
  199.                 + BMPlenbits(class, bitcount, x, y);
  200. }
  201.  
  202. #endif /* _BMP_H_ */
  203.  
  204.